package com.ebocy.hammer.core.db;

import com.ebocy.hammer.core.db.mongo.MongoConnection;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBObject;

public class AutoIncreaseID {
	/** 玩家ID */
	public static final String GAME_PLAYER_ID = "gamePlayerId";
	/** 物品ID */
	public static final String GAME_PLAYER_ITEM_ID = "gamePlayerItemId";

	private AutoIncreaseID() {
	}

	public static AutoIncreaseID getInstance() {
		return SingletonClassInstance.instance;
	}

	private static class SingletonClassInstance {
		private static final AutoIncreaseID instance = new AutoIncreaseID();
	}

    public Long getAutoIncreaseID(String idName) {
        BasicDBObject query = new BasicDBObject();
        query.put("name", idName);
        BasicDBObject update = new BasicDBObject();
        update.put("$inc", new BasicDBObject("id", 1l));
        DB db = MongoConnection.getInstance().getDB();
        DBObject dbObject = db.getCollection("inc_ids").findAndModify(query, null, null, false, update, true, true);
        Long id = (Long) dbObject.get("id");
        return id;
    }
}
